package com.millstein.tsinglog.controller;

import com.millstein.tsinglog.annotation.ResponseResult;
import com.millstein.tsinglog.annotation.ShiroAccessLog;
import com.millstein.tsinglog.model.dto.category.CategorySaveDTO;
import com.millstein.tsinglog.model.pojo.Category;
import com.millstein.tsinglog.model.vo.result.PageResult;
import com.millstein.tsinglog.service.CategoryService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
@ResponseResult
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有分类的信息
     * @return
     */
    @GetMapping("/getAllCategory")
    @RequiresPermissions(value = {"article:insert", "article:update", "tag:query", "article:query"},
            logical = Logical.OR)
    public List<Category> getAllCategory() {
        List<Category> list = categoryService.getAllCategory();
        return list;
    }

    /**
     * 分页条件查询分类信息
     * @param currentPage 当前页
     * @param pageSize 每页的数据量
     * @param keyword 搜索关键字
     * @return
     */
    @GetMapping("/getCategoryByPage")
    @RequiresPermissions("category:query")
    @ShiroAccessLog("查询分类信息")
    public PageResult<Category> getCategoryByPage(Integer currentPage,
                                                  Integer pageSize,
                                                  String keyword) {
        return categoryService.getCategoryByPage(currentPage, pageSize, keyword);
    }

    /**
     * 新增分类
     * @param categorySaveDTO 新增分类信息封装的对象
     */
    @PostMapping("/insertCategory")
    @RequiresPermissions("category:insert")
    @ShiroAccessLog("新增分类信息")
    public void insertCategory(@Valid @RequestBody CategorySaveDTO categorySaveDTO) {
        categoryService.insertCategory(categorySaveDTO);
    }

    /**
     * 根据ids删除对应分类的信息（假删除）
     * @param ids 存放需删除分类的id的集合
     */
    @GetMapping("/deleteByIds")
    @RequiresPermissions("category:delete")
    @ShiroAccessLog("删除分类信息")
    public void deleteByIds(@RequestParam("ids") List<Long> ids) {
        categoryService.deleteByIds(ids);
    }

    /**
     * 设置指定分类的启用状态
     * @param id 该分类的id
     * @param usable 是否启用，1为启用，0为不启用
     */
    @GetMapping("/changeUsable")
    @RequiresPermissions("category:update")
    @ShiroAccessLog("修改分类信息")
    public void changeUsable(Long id, Short usable) {
        categoryService.changeUsable(id, usable);
    }

    /**
     * 根据id查询分类信息
     * @param id 分类id
     * @return
     */
    @GetMapping("/getCategoryById")
    @RequiresPermissions("category:update")
    public Category getCategoryById(Long id) {
        return categoryService.getCategoryById(id);
    }

    /**
     * 更新分类信息
     * @param categorySaveDTO 更新分类信息封装的实体
     */
    @PostMapping("/updateCategory")
    @RequiresPermissions("category:update")
    @ShiroAccessLog("修改分类信息")
    public void updateCategory(@Valid @RequestBody CategorySaveDTO categorySaveDTO) {
        categoryService.updateCategory(categorySaveDTO);
    }
}


